using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class RecordPanel : MonoBehaviour
{
    public GameObject m_TxtTemplate;
    public GameObject m_JuniorLayout;
    public GameObject m_SeniorLayout;
    public GameObject m_ExpertLayout;

    public void Show()
    {
        gameObject.SetActive(true);

        if (m_Init) return;

        List<int> records1 = RecordManager.Instance.GetRecord(EGameMode.Junior);
        UpdateRecord(records1, m_JuniorLayout);
        List<int> records2 = RecordManager.Instance.GetRecord(EGameMode.Senior);
        UpdateRecord(records2, m_SeniorLayout);
        List<int> records3 = RecordManager.Instance.GetRecord(EGameMode.Expert);
        UpdateRecord(records3, m_ExpertLayout);
        m_Init = true;
    }

    public void Hide()
    {
        gameObject.SetActive(false);
    }


    private bool m_Init;

    void UpdateRecord(List<int> records, GameObject parentGo)
    {
        for (int i = 0; i < records.Count; i++)
        {
            GameObject obj = GameObject.Instantiate(m_TxtTemplate);
            obj.SetActive(true);
            obj.transform.SetParent(parentGo.transform);
            obj.transform.localScale = Vector3.one;
            Text item = obj.GetComponent<Text>();

            item.text = string.Format("{0}、{1}", i + 1, FormatTimeSeconds(records[i]));
        }

        RectTransform rect = parentGo.GetComponent<RectTransform>();
        RectTransform templateRect = m_TxtTemplate.GetComponent<RectTransform>();

        rect.sizeDelta = new Vector2(rect.sizeDelta.x, (records.Count + 1) * templateRect.rect.height);
    }

    string FormatTimeSeconds(int passTime)
    {
        int minute = passTime / 60;
        int second = passTime % 60;
        return string.Format("{0}分{1}秒", minute, second);
    }

    void Awake()
    {
        gameObject.SetActive(false);

        Button btn = GetComponent<Button>();
        btn.onClick.AddListener(() =>
        {
            Hide();
        });
    }
}
